package pikasial.images;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import pikasial.images.tags.*;


@SuppressWarnings("serial")
public class TagsImage extends javax.swing.DefaultListModel {
	protected TagLieu tagLieu;
    protected TagPersonne tagPerso;
    protected TagEvenement tagEvent;
    protected List<TagPerso> tagsPerso;
    protected int nbTags;

    public static final int MAX_TAGS_PERSO = 10;

    public TagsImage() {
    	super();
    	tagsPerso = new ArrayList<TagPerso>(MAX_TAGS_PERSO);
    	nbTags = 0;
    }

    public Collection<TagPerso> getTagsPerso() {
    	return tagsPerso;
    }

    public TagLieu getTagLieu() {
    	return tagLieu;
    }

    public int getNombreTagsPerso() { return tagsPerso.size(); }

    public TagPersonne getTagPersonne() { return tagPerso; }
    public TagEvenement getTagEvenement() { return tagEvent; }

    public Collection<Tag> getAllTags() {
    	Collection<Tag> c = new ArrayList<Tag>();
    	if (tagLieu != null) c.add(tagLieu);
    	if (tagPerso != null) c.add(tagPerso);
    	if (tagEvent != null) c.add(tagEvent);
    	if (!tagsPerso.isEmpty()) c.addAll(tagsPerso);
    	return c;
    }

    public void setTagLieu(TagLieu tag) {
    	if (tagLieu == null && tag != null) nbTags++;
    	else if (tagLieu != null && tag == null) nbTags--;
    	tagLieu = tag;
    	fireContentsChanged(this,tagsPerso.size(),nbTags-1);
    }
    public void setTagEvenement(TagEvenement tag) {
    	if (tagEvent == null && tag != null) nbTags++;
    	else if (tagEvent != null && tag == null) nbTags--;
    	tagEvent = tag;
    	fireContentsChanged(this,tagsPerso.size(),nbTags-1);
    }

    public void setTagPersonne(TagPersonne tag) {
    	if (tagPerso == null && tag != null) nbTags++;
    	else if (tagPerso != null && tag == null) nbTags--;
    	tagPerso = tag;
    	fireContentsChanged(this, tagsPerso.size(), nbTags-1);
    }

    public void addTagPerso(TagPerso tag) {
    	if (!tagsPerso.contains(tag) && (tagsPerso.size() < MAX_TAGS_PERSO-1)) {
    		tagsPerso.add(tag);
    		nbTags++;
    		fireContentsChanged(this, 0, tagsPerso.size());
    	}
    }

    public void addTag(Tag tag) {
    	if (tag != null) {
    		if (tag instanceof TagPerso)
    			addTagPerso((TagPerso) tag);
    		else if (tag instanceof TagPersonne)
    			setTagPersonne((TagPersonne) tag);
    		else if (tag instanceof TagEvenement)
    			setTagEvenement((TagEvenement) tag);
    		else if (tag instanceof TagLieu)
    			setTagLieu((TagLieu) tag);
    	}
    }

    public void removeTag(Tag tag) {
    	int index = indexOf(tag);
    	if (index != -1) {
    		if (tag instanceof TagPerso)
    			removeTagPerso(index);
    		else if (tag instanceof TagEvenement)
    			setTagEvenement(null);
    		else if (tag instanceof TagPersonne)
    			setTagPersonne(null);
    		else if (tag instanceof TagLieu)
    			setTagLieu(null);
    	}
    }

    public void removeTagPerso(int index) {
    	tagsPerso.remove(index);
    	fireContentsChanged(this,0,tagsPerso.size());
    	nbTags--;
    }

    public void removeAllTagsPerso() {
    	int size = nbTags;
    	tagsPerso.clear();
    	nbTags = 0;
    	fireIntervalRemoved(this,0,size);
    }

    public TagPerso getTagPerso(int index) {
    	return tagsPerso.get(index);
    }


	public int getSize() {
		return nbTags;
	}

	public Object getElementAt(int index) {
		int sizePerso = tagsPerso.size();
		if (index < sizePerso && index >= 0)
			return tagsPerso.get(index);
		else if (index >= sizePerso && index < nbTags) {
			int goal = index - sizePerso;
			int i = 0;
			if (tagLieu != null) {
				if (i == goal)
					return tagLieu;
				else i++;
			}
			if (tagEvent != null) {
				if (i == goal)
					return tagEvent;
				else i++;
			}
			if (tagPerso != null) {
				if (i == goal)
					return tagPerso;
				else i++;
			}
			return null;
		} else {
			return null;
		}
	}

	public int indexOf(Object elem) {
		int sizePerso = tagsPerso.size();
		if (elem instanceof TagPerso)
			return tagsPerso.indexOf((TagPerso) elem);
		else if (elem instanceof Tag) {
			int i = sizePerso;
			if (tagLieu != null && tagLieu != elem)
				i++;
			if (tagEvent != null && tagEvent != elem)
				i++;
			if (tagPerso != null && tagPerso != elem)
				i++;
			return (i >= nbTags)?-1:i;

		} else {
			return -1;
		}
	}
}
